﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Telerik.Web.UI;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.BasicService;
using System.Web.Services;

namespace RN.ERP.Web.Person
{
    public partial class Exam : BasePage
    {
        #region  公共属性
        protected string CorpUserGuid
        {
            get
            {
                return UserInfo.CorpUserGuid;
            }
        }
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        protected string RoleType
        {
            get
            {
                return UserInfo.RoleType;
            }
        }
        /// <summary>
        /// 人员类型
        /// </summary>
        private string classId;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initial();
            }
        }
        /// <summary>
        /// 参数及设置按钮显示
        /// </summary>
        private void Initial()
        {
            ////人员类型
            if (Request.QueryString["classId"] != null)
            {
                classId = Request.QueryString["classId"];
            }
        }
        /// <summary>
        /// 取得人员信息参数列表
        /// </summary>
        /// <returns></returns>
        private string GetQueryConditions()
        {

            List<ModelService.QueryExamInfoCondition> query = new List<ModelService.QueryExamInfoCondition>();
            ModelService.QueryExamInfoCondition queryCondition = GetQueryCondition("1,");
            query.Add(queryCondition);
            return JsonConvert.SerializeObject(query);
        }
        /// <summary>
        /// 取得人员参数项
        /// </summary>
        /// <param name="flowCode"></param>
        /// <returns></returns>
        private ModelService.QueryExamInfoCondition GetQueryCondition(string flowCode)
        {
            ///设置查询参数
            ModelService.QueryExamInfoCondition queryCondition = new ModelService.QueryExamInfoCondition();
            //queryCondition.TypeFlag = new ParameterValue<string> { V = "-1", O = OptEnum.Like };
            if (!string.IsNullOrEmpty(classId))
            {
                queryCondition.ClassId = new ParameterValue<string> { V = classId, O = OptEnum.Equal };
            }
            if (!string.IsNullOrEmpty(flowCode))
            {
                queryCondition.FlowCode = new ParameterValue<string> { V = flowCode, O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(txtEmpName.Text.Trim()))
            {
                queryCondition.Name = new ParameterValue<string> { V = txtEmpName.Text.Trim(), O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(txtEmpIdCard.Text.Trim()))
            {
                queryCondition.IDCard = new ParameterValue<string> { V = txtEmpIdCard.Text.Trim(), O = OptEnum.Equal };
            }
            Guid userGuid = Guid.Empty;
            Guid.TryParse(UserInfo.CorpUserGuid, out userGuid);
            queryCondition.CorpUserGuid = new ParameterValue<Guid> { V = userGuid, O = OptEnum.Equal };
            return queryCondition;
        }
        /// <summary>
        /// 取得人员基本信息
        /// </summary>
        /// <param name="empGuid"></param>
        /// <param name="areaCode"></param>
        private List<ModelService.Emp_ExamInfo> GetEmpManagersList(string areaCode, int currentPageIndex, int pageSize)
        {
            try
            {
                using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
                {
                    string queryStr = GetQueryConditions();

                    rgPersonList.VirtualItemCount = client.GetReportExamListCount(queryStr, areaCode);
                    string result = client.GetReportExamList(currentPageIndex + 1, pageSize, queryStr, areaCode);
                    return JsonConvert.DeserializeObject<List<ModelService.Emp_ExamInfo>>(result);
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 绑定人员数据列表
        /// </summary>
        private void rgPersonDataBind()
        {
            int currentPageIndex = rgPersonList.CurrentPageIndex;
            int pageSize = rgPersonList.MasterTableView.PageSize;
            rgPersonList.DataSource = GetEmpManagersList(AreaCode, currentPageIndex, pageSize);

        }
        /// <summary>
        /// 绑定查询列表
        /// </summary>
        private void PersonDataBind()
        {
            rgPersonList.CurrentPageIndex = 0;
            rgPersonDataBind();
            rgPersonList.DataBind();
        }

        /// <summary>
        /// 绑定人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void rgPersonList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            rgPersonDataBind();

        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnEmpSreach_Click(object sender, EventArgs e)
        {
            PersonDataBind();
        }

        [WebMethod]
        public static string AddExam(object empguid, object corpguid, object AreaCode)
        {
            string ret = null;
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                bool result = client.ReportExam(empguid.ToString(), corpguid.ToString(), AreaCode.ToString());
                ret = JsonConvert.SerializeObject(new { Message = result ? "报名成功！" : "该人员已报名！" });
            }
            return ret;
        }
    }
}